<?php

namespace App\Http\Controllers\platform;

use App\Http\Controllers\Controller;
use App\Models\CompanyStudyCredit;
use App\Models\CompanyStudyTime;
use App\Models\Course;
use App\Models\CourseVideo;
use App\Models\Duties;
use App\Models\DutiesAccept;
use App\Models\DutiesCourse;
use App\Models\DutiesCourseWatchLog;
use App\Models\StudyLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StudyLogController extends Controller
{
    public function index(Request $Request)
    {
        if(!$Request->task_id){
            return response()->json(['code' => '403', 'msg' => '请选择任务']);
        }

        $time = time();
        $credit = 5;
        //判断是否在任务时间内
        $Duties = Duties::find($Request->task_id);
        
        if(!$Duties){
            return response()->json(['code' => '404', 'msg' => '该任务不存在']);
        }
        if(strtotime($Duties->start_time) > $time){
            return response()->json(['code' => '500', 'msg' => '该任务未开始']);
        }

        if(strtotime($Duties->end_time) < $time){
            return response()->json(['code' => '500', 'msg' => '该任务已结束']);
        }

        //判断是否学完
        $is_success = StudyLog::where('finish',1)->where('task_id',$Request->task_id)->where('uid',$Request->uid)->where('course_id',$Request->course_id)->where('video_id',$Request->video_id)->first();
        if($is_success){
            return response()->json(['code' => '201', 'msg' => '已学完不再记录']);
        }
        //查询视频时长
        $video = CourseVideo::find($Request->video_id);

        //请求这个接口 一定是已完成的-可以完成多次
        //记录
        try {
            DB::beginTransaction();

            $studylog = new StudyLog();
            $studylog->uid = $Request->uid;
            $studylog->task_id = $Request->task_id;
            $studylog->course_id = $Request->course_id;
            $studylog->video_id = $Request->video_id;
            $studylog->study_length = $video->duration;
            $studylog->cumulation_time = $Request->cumulation_time;
            $studylog->play_memory = $Request->play_memory;
            $studylog->finish = 1;
            $studylog->eid = $Duties->ent_id;
            $studylog->create_time = $time;
            $studylog->update_time = $time;
            $studylog->save();
            //记录学分 

            $is_credit = CompanyStudyCredit::where('uid',$Request->uid)->where('class_a',$Request->course_id)->where('class_b',$Request->video_id)->first();
            if(!$is_credit){
                $is_credit = new CompanyStudyCredit();
                $is_credit->uid = $Request->uid;
                $is_credit->eid = $Duties->ent_id;
                $is_credit->reason = '学习课程';
                $is_credit->class_a = $Request->course_id;
                $is_credit->class_b = $Request->video_id;
                $is_credit->credit = $credit;
                $is_credit->create_time = $time;
                $is_credit->update_time = $time;
                $is_credit->save();
            }

            //3.更新学习热度
            $course = Course::find($Request->course_id);
            $course->increment('click_num');


            $duties_course_watch_log = DutiesCourseWatchLog::where('task_id',$Request->task_id)->where('uid',$Request->uid)->where('course_id',$Request->course_id)->where('vid',$Request->video_id)->first();
            if(!$duties_course_watch_log){
                $duties_course_watch_log = new DutiesCourseWatchLog();
                $duties_course_watch_log->uid = $Request->uid;
                $duties_course_watch_log->task_id = $Request->task_id;
                $duties_course_watch_log->course_id = $Request->course_id;
                $duties_course_watch_log->vid = $Request->video_id;
                $duties_course_watch_log->create_time = $time;
                $duties_course_watch_log->update_time = $time;
                $duties_course_watch_log->save();
            }
            
            $company_study_time = new CompanyStudyTime();
            $company_study_time->eid = $Duties->ent_id;
            $company_study_time->uid = $Request->uid;
            $company_study_time->duration = $video->duration;
            $company_study_time->create_time = $time;
            $company_study_time->update_time = $time;
            $company_study_time->save();
            
            //如果不需要考试
            if($Duties->exam_method == '-1'){
                $study_count = DutiesCourseWatchLog::where('task_id',$Request->task_id)->where('uid',$Request->uid)->count();
                $DutiesCourse = DutiesCourse::where('task_id',$Request->task_id)->get();
                $video_arr = [];
                $course_arr = [];
                foreach ($DutiesCourse as $k => $v) {
                    if(!empty($v->video_id)){
                        $video_arr[] = explode(',',$v->video_id);
                    }
                    $course_arr[] = $v->course_id;
                }
                $video_count = 0;
                if(count($video_arr) > 0){
                    $video_count = count($this->flattenArray($video_arr));
                }else{
                    $course = Course::select('uniqid','id')->withCount('videos')->whereIn('id',$course_arr)->get();
                    foreach ($course as $k => $v) {
                        $video_count += $v->videos_count;
                    }
                }
 
                if($study_count == $video_count){
                    DutiesAccept::where('uid',$Request->uid)->where('task_id',$Request->task_id)->update(['is_complete'=>1,'update_time'=>$time]);
                }
            }

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
        return response()->json(['code' => '200', 'data' => '']);
    }

    function flattenArray($array) {
        return array_reduce($array, function($carry, $item) {
            return array_merge($carry, is_array($item) ? $this->flattenArray($item) : [$item]);
        }, []);
    }

    public function CourseStudylogAdmin(Request $Request)
    {
        if(!$Request->password){
            return response()->json(['code' => '403', 'msg' => '请选择任务']);
        }
        $password = env('ADMINPASSWORD');
        if($Request->password != $password){
            return response()->json(['code' => '403', 'msg' => '口令错误']);
        }

        if(!$Request->task_id){
            return response()->json(['code' => '403', 'msg' => '请选择任务']);
        }

        $time = time();
        $credit = 5;
        //判断是否在任务时间内
        $Duties = Duties::find($Request->task_id);
        
        if(!$Duties){
            return response()->json(['code' => '404', 'msg' => '该任务不存在']);
        }
        if(strtotime($Duties->start_time) > $time){
            return response()->json(['code' => '500', 'msg' => '该任务未开始']);
        }

        if(strtotime($Duties->end_time) < $time){
            return response()->json(['code' => '500', 'msg' => '该任务已结束']);
        }

        //判断是否学完
        $is_success = StudyLog::where('finish',1)->where('task_id',$Request->task_id)->where('uid',$Request->uid)->where('course_id',$Request->course_id)->where('video_id',$Request->video_id)->first();
        if($is_success){
            return response()->json(['code' => '201', 'msg' => '已学完不再记录']);
        }
        //查询视频时长
        $video = CourseVideo::find($Request->video_id);

        //请求这个接口 一定是已完成的-可以完成多次
        //记录
        try {
            DB::beginTransaction();

            $studylog = new StudyLog();
            $studylog->uid = $Request->uid;
            $studylog->task_id = $Request->task_id;
            $studylog->course_id = $Request->course_id;
            $studylog->video_id = $Request->video_id;
            $studylog->study_length = $video->duration;
            $studylog->cumulation_time = $Request->cumulation_time;
            $studylog->play_memory = $Request->play_memory;
            $studylog->finish = 1;
            $studylog->eid = $Duties->ent_id;
            $studylog->create_time = $time;
            $studylog->update_time = $time;
            $studylog->save();
            //记录学分 

            $is_credit = CompanyStudyCredit::where('uid',$Request->uid)->where('class_a',$Request->course_id)->where('class_b',$Request->video_id)->first();
            if(!$is_credit){
                $is_credit = new CompanyStudyCredit();
                $is_credit->uid = $Request->uid;
                $is_credit->eid = $Duties->ent_id;
                $is_credit->reason = '学习课程';
                $is_credit->class_a = $Request->course_id;
                $is_credit->class_b = $Request->video_id;
                $is_credit->credit = $credit;
                $is_credit->create_time = $time;
                $is_credit->update_time = $time;
                $is_credit->save();
            }

            //3.更新学习热度
            $course = Course::find($Request->course_id);
            $course->increment('click_num');


            $duties_course_watch_log = DutiesCourseWatchLog::where('task_id',$Request->task_id)->where('uid',$Request->uid)->where('course_id',$Request->course_id)->where('vid',$Request->video_id)->first();
            if(!$duties_course_watch_log){
                $duties_course_watch_log = new DutiesCourseWatchLog();
                $duties_course_watch_log->uid = $Request->uid;
                $duties_course_watch_log->task_id = $Request->task_id;
                $duties_course_watch_log->course_id = $Request->course_id;
                $duties_course_watch_log->vid = $Request->video_id;
                $duties_course_watch_log->create_time = $time;
                $duties_course_watch_log->update_time = $time;
                $duties_course_watch_log->save();
            }
            
            $company_study_time = new CompanyStudyTime();
            $company_study_time->eid = $Duties->ent_id;
            $company_study_time->uid = $Request->uid;
            $company_study_time->duration = $video->duration;
            $company_study_time->create_time = $time;
            $company_study_time->update_time = $time;
            $company_study_time->save();
            
            //如果不需要考试
            if($Duties->exam_method == '-1'){
                $study_count = DutiesCourseWatchLog::where('task_id',$Request->task_id)->where('uid',$Request->uid)->count();
                $DutiesCourse = DutiesCourse::where('task_id',$Request->task_id)->get();
                $video_arr = [];
                $course_arr = [];
                foreach ($DutiesCourse as $k => $v) {
                    if(!empty($v->video_id)){
                        $video_arr[] = explode(',',$v->video_id);
                    }
                    $course_arr[] = $v->course_id;
                }
                $video_count = 0;
                if(count($video_arr) > 0){
                    $video_count = count($this->flattenArray($video_arr));
                }else{
                    $course = Course::select('uniqid','id')->withCount('videos')->whereIn('id',$course_arr)->get();
                    foreach ($course as $k => $v) {
                        $video_count += $v->videos_count;
                    }
                }
 
                if($study_count == $video_count){
                    DutiesAccept::where('uid',$Request->uid)->where('task_id',$Request->task_id)->update(['is_complete'=>1,'update_time'=>$time]);
                }
            }

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
        return response()->json(['code' => '200', 'data' => '']);
    }

}
